package root.tools.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet Filter implementation class Filter1
 */

public class Filter1 implements Filter {
	String str = null;
	
    public Filter1() {
    }

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//		还原回http的  req res
		HttpServletRequest req =(HttpServletRequest)request;
		HttpServletResponse res = (HttpServletResponse) response;
		req.setCharacterEncoding(str);
		res.setCharacterEncoding(str);
//		过滤器 本身就是为了拦截程序员不想处理的请求，
//		比如说网络攻击，数据盗取，账号盗用,一般情况下这种过滤器会放行也有不放行的情况
		
//		其次 还有 对用户数据的清洗，字符集编码，无论如何都会放行
//		if() {
//		}else {
//		}
//		放行，
		System.out.println("Filter1 start");
		chain.doFilter(request, response);
		System.out.println("Filter1 end");
	}

	public void init(FilterConfig fConfig) throws ServletException {
		str = fConfig.getInitParameter("encoding");
		if(str==null||str.equals("")) str = "utf-8";
	}

}
